option page form in my wordpress theme [migrated]

Posted by Templategraphy on Pro Webmasters See other posts from Pro Webmasters or by Templategraphy
Published on 2014-06-10T07:32:15Z Indexed on 2014/06/10 9:42 UTC
Read the original article Hit count: 144

Filed under:
|

here its is my option page code containing no of fields like logo, slider after filling all the information in option page form i want to things

  1. After submitting all the form details save information must retain there.

  2. Using get_option() extract each input tag value and show that value in front hand like slider image, slider heading, slider description

    OPTION PAGE CODE:
    
    <?php
    class MySettingsPage
    {
    
        private $options;
    
         public function __construct()
        {
            add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
            add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
    
    
        }
    
             public function bguru_register_options_page()
        {
            // This page will be under "Settings"
         add_theme_page('Business Guru Options',
                         'Theme Customizer', 
                         'edit_theme_options', 
                         'bguru-options', 
                         array( $this, 'bguru_options_page')
            );
        }
    
    public function bguru_options_page()
        {
            // Set class property
             $this->options = get_option( 'bguru_logo' );
             $this->options = get_option( 'bguru_vimeo' );
             $this->options = get_option( 'bguru_slide_one_image' );
             $this->options = get_option( 'bguru_slide_one_heading' );
             $this->options = get_option( 'bguru_slide_one_text' );
             $this->options = get_option( 'bguru_slogan_heading' );
             $this->options = get_option( 'bguru_slogan_description' );
    
    
            ?>
            <div class="wrap">
                <?php screen_icon(); ?>
              <h1>Business Guru Options</h1>      
                <form method="post" action="options.php">
                    <table class="form-table">
                <?php
                    // This prints out all hidden setting fields
                    settings_fields( 'defaultbg' );   
                    do_settings_sections( 'defaultbg' );
                    submit_button(); 
                ?>
                    </table>
                </form>
            </div>
            <?php
        }
    
     /**
         * Register and add settings
         */
        public function bguru_register_settings()
        {        
    register_setting('defaultbg','bguru_logo',  array( $this, 'sanitize' ) );
    register_setting('defaultbg', 'bguru_vimeo',  array( $this, 'sanitize' ));
    register_setting('defaultbg', 'bguru_slide_one_image',  array( $this, 'sanitize' ));
    register_setting('defaultbg', 'bguru_slide_one_heading',  array( $this, 'sanitize' ));
    register_setting('defaultbg', 'bguru_slide_one_text',  array( $this, 'sanitize' ));
    register_setting('defaultbg', 'bguru_slogan_heading',  array( $this, 'sanitize' ));
     register_setting('defaultbg', 'bguru_slogan_description',  array( $this, 'sanitize' ));
           add_settings_section(
                'setting_section_id', // ID
                '<h2>General</h2>', 
                array( $this, 'print_section_info' ), // Callback
                'defaultbg' // Page
    
            );  
    
            add_settings_field( 
    
                'bguru_logo', // ID
                '<label for="bguru_logo">Logo</label>', // Title 
                array($this,'logo_callback' ), // Callback
                 'defaultbg', // Page
               'setting_section_id'// Section       
    
            );      
    
    
            add_settings_field(
                'bguru_vimeo', // ID
                'Vimeo', // Vimeo 
                array( $this, 'socialv_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
            add_settings_field(
                'bguru_slide_one_image', // ID
                'Slide 1 Image', // Slide 1 Image 
                array( $this, 'slider1img_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
            add_settings_field(
                'bguru_slide_one_heading', // ID
                'Slide 1 Heading', // Slide 1 Heading 
                array( $this, 'slider1head_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
            add_settings_field(
                'bguru_slide_one_text', // ID
                'Slide 1 Description', // Slide 1 Description
                array( $this, 'slider1text_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
            add_settings_field(
                'bguru_slogan_heading', // ID
                'Slogan Heading', // Slogan Heading
                array( $this, 'slogan_head_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
            add_settings_field(
                'bguru_slogan_description', // ID
                'Slogan Container', // Slogan Container
                array( $this, 'slogan_descr_callback' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );
    
        }
    public function sanitize( $input )
        {
            $new_input = array();
             if( isset( $input['bguru_logo'] ) )
                $new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
    
                 if( isset( $input['bguru_vimeo'] ) )
                $new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
    
                 if( isset( $input['bguru_slide_one_image'] ) )
                $new_input['bguru_slide_one_image'] = sanitize_text_field( $input['bguru_slide_one_image'] );
    
                 if( isset( $input['bguru_slide_one_heading'] ) )
                $new_input['bguru_slide_one_heading'] = sanitize_text_field( $input['bguru_slide_one_heading'] ); 
    
                if( isset( $input['bguru_slide_one_text'] ) )
                $new_input['bguru_slide_one_text'] = sanitize_text_field( $input['bguru_slide_one_text'] );
    
                if( isset( $input['bguru_slogan_heading'] ) )
                $new_input['bguru_slogan_heading'] = sanitize_text_field( $input['bguru_slogan_heading'] );
    
                 if( isset( $input['bguru_slogan_description'] ) )
                $new_input['bguru_slogan_description'] = sanitize_text_field( $input['bguru_slogan_description'] );
    
                 return $new_input;
        }
    public function print_section_info()
        {
            print 'Enter your settings below:';
        }
    
    public function logo_callback()
        {
    
            printf(
                '<input type="text" id="bguru_logo"  size="50" name="bguru_logo" value="%s" />',
                isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
            );
        }
    
    public function socialv_callback()
        {
            printf(
                '<input type="text" id="bguru_vimeo"  size="50" name="bguru_vimeo" value="%s" />',
                isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
            );
        }
    
    
        public function slider1img_callback()
        {
            printf(
                '<input type="text" id="bguru_slide_one_image"  size="50" name="bguru_slide_one_image" value="%s" />',
                isset( $this->options['bguru_slide_one_image'] ) ? esc_attr( $this->options['bguru_slide_one_image']) : ''
            );
        }
        public function  slider1head_callback()
        {
            printf(
                '<input type="text" id="bguru_slide_one_heading"  size="50" name="bguru_slide_one_heading" value="%s" />',
                isset( $this->options['bguru_slide_one_heading'] ) ? esc_attr( $this->options['bguru_slide_one_heading']) : ''
            );
        }
        public function slider1text_callback()
        {
            printf(
                '<input type="text" id="bguru_slide_one_text"  size="50" name="bguru_slide_one_text" value="%s" />',
                isset( $this->options['bguru_slide_one_text'] ) ? esc_attr( $this->options['bguru_slide_one_text']) : ''
            );
        }
    
    
        public function slogan_head_callback()
        {
            printf(
                '<input type="text" id="bguru_slogan_heading"  size="50" name="bguru_slogan_heading" value="%s" />',
                isset( $this->options['bguru_slogan_heading'] ) ? esc_attr( $this->options['bguru_slogan_heading']) : ''
            );
        }
    
        public function slogan_descr_callback()
        {
            printf(
                '<input type="text" id="bguru_slogan_description"  size="50" name="bguru_slogan_description" value="%s" />',
                isset( $this->options['bguru_slogan_description'] ) ? esc_attr( $this->options['bguru_slogan_description']) : ''
            );
        }
     }
    if( is_admin() )
        $my_settings_page = new MySettingsPage();
    

    here its my header.php code where i display all the information of option form

    $bguru_logo_image = get_option('bguru_logo');

    if (!empty($bguru_logo_image)) {
    
        echo '<div id="logo"><a href="' . home_url() . '"><img src="' . $bguru_logo_image . '" width="218" alt="logo" /></a></div><!--/ #logo-->';
    
    } else {
    
        echo '<div id="logo"><a href="' . home_url() . '"><h1>'. get_bloginfo('name') . '</h1></a></div><!--/ #logo-->';
    
    }?>
    

    $bguru_social_vimeo = get_option('bguru_vimeo');
    

    if (!empty($bguru_social_vimeo)) {

        echo '<li class="vimeo"><a target="_blank" href="'.$bguru_social_vimeo.'">Vimeo</a></li>';
    
    }?>
    

    same as for slider image, slider heading, slider description please suggest some solutions

© Pro Webmasters or respective owner

Related posts about php

Related posts about Wordpress